package com.example.RuanJianKeShe.Service.manager.get;

import com.alibaba.fastjson.JSON;
import com.example.RuanJianKeShe.Bean.Course;
import com.example.RuanJianKeShe.Bean.SC;
import com.example.RuanJianKeShe.Bean.Student;
import com.example.RuanJianKeShe.Dao.impl.CourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.StuCourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.StudentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/getAllSelectCourses")
public class getAllSelectCourses extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        StuCourseDaoImpl stuCourseDao = new StuCourseDaoImpl();
        List<SC> allSC = stuCourseDao.getAllSC();
        CourseDaoImpl courseDao = new CourseDaoImpl();
        List<Course> allCourse = courseDao.getAllCourse();
        StudentDaoImpl studentDao = new StudentDaoImpl();
        List<Student> allStudent = studentDao.getAllStudent();
        HashMap<String,Object>[] arr = new HashMap[allSC.size()];
        HashMap<String,Object> map;
        for(int i=0;i<allSC.size();i++){
            map = new HashMap<>();
            for (int j=0;j<allStudent.size();j++){
                if (allStudent.get(j).getSno()==allSC.get(i).getSno()){
                    map.put("sno",allStudent.get(j).getSno());
                    map.put("sname",allStudent.get(j).getSname());
                }
            }
            for (int j=0;j<allCourse.size();j++){
                if (allCourse.get(j).getCno()==allSC.get(i).getCno()){
                    map.put("cno",allCourse.get(j).getCno());
                    map.put("cname",allCourse.get(j).getCname());
                }
            }
            map.put("grade",allSC.get(i).getGrade());
            arr[i]=map;
        }
        HashMap<String,Object> resultMap = new HashMap<>();
        resultMap.put("allStudentCourses",arr);
        String jsonString = JSON.toJSONString(resultMap);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
